/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.survey;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;

import twaver.Element;
import twaver.chart.PercentChart;
import demo.DemoPane;

public class SurveyDemo extends DemoPane{
	
	public final static Color color = new Color(102, 153, 255);
	public final static Color background = new Color(238, 238, 200);
	
	public SurveyDemo(){
		GridLayout layout = new GridLayout(1, 2);
		layout.setHgap(10);
        JPanel topPane = new JPanel(layout);  
        topPane.add(createSortablePane(new PhaseChart()));
        topPane.add(createSortablePane(new ConjunctionChart()));
        JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPane, new FrameworkChart());
        mainPane.setDividerLocation(250);
        this.add(mainPane);
	}
	
	private JPanel createSortablePane(final PercentChart chart){
		JPanel buttonPane = new JPanel();
		
		JRadioButton ascend = new JRadioButton("Ascend");
		JRadioButton descend = new JRadioButton("Descend");
		JRadioButton def = new JRadioButton("Default", true);
		
		buttonPane.add(ascend);
		buttonPane.add(descend);
		buttonPane.add(def);
		
		ButtonGroup group = new ButtonGroup();
		group.add(ascend);
		group.add(descend);
		group.add(def);
		
		ascend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setSortComparator(new Comparator(){
					public int compare(Object o1, Object o2) {
						Element e1 = (Element)o1;
						Element e2 = (Element)o2;
						return (int)(e1.getChartValue() - e2.getChartValue());
					}
				});
			}
		});
		descend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setSortComparator(new Comparator(){
					public int compare(Object o1, Object o2) {
						Element e1 = (Element)o1;
						Element e2 = (Element)o2;
						return (int)(e2.getChartValue() - e1.getChartValue());
					}
				});
			}
		});
		def.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setSortComparator(null);
			}
		});
		
		JPanel pane = new JPanel(new BorderLayout());
		pane.add(chart, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		return pane;
	}
}